// pages/my/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        startX: 0,
        endX: 0,
        iCenter: 0,
        order: [],
        showPopup: false,
        name: "",
        idCard: "",
        errorFlag: false,
        errorIdCardFlag: false,
        child_id: "",
        userInfo: "",
        child: [],
        user_id: "",
        childInfo: '',
        telephone: '',
        showIfVipPopup: false,
        total: ""
    },
    onCloseIfVipPopup() {
        this.setData({
            showIfVipPopup: false
        })
    },
    checkVIP() {
        http.postHttp(api.checkVIP, {
                personal_id: this.data.childInfo.personal_id,
                name: this.data.childInfo.name
            },
            (result) => {
                if (result.code == 0) {
                    if (!result.data.if_vip) {
                        this.setData({
                            showIfVipPopup: true
                        })
                    } else {
                        wx.navigateTo({
                            url: '/pages/activationInfo/index?childname=' + this.data.childInfo.name +
                                '&idcard=' + this.data.childInfo.personal_id + '&phone=' + this.data.telephone +
                                '&from=my' + '&truename=' + this.data.userInfo.name,
                        })
                    }
                }

            },
            (err) => {
                console.log(err);
            },
        )
    },
    /** 添加孩子 */
    addChild() {
        this.setData({
            showPopup: true
        })
    },
    onClosePopup() {
        this.setData({
            showPopup: false,
            errorFlag: false,
            errorIdCardFlag: false,
            name: "",
            idCard: ""
        })
    },
    /** 提交添加孩子 */
    submitAddFun() {
        if (this.data.name.trim() == '') {
            this.setData({
                errorFlag: true
            })
            return
        }
        if (this.data.idCard.trim() == '') {
            this.setData({
                errorIdCardFlag: true
            })
            return
        }
        let obj = {
            name: this.data.name,
            personal_id: this.data.idCard,
            user_id: this.data.user_id
        }
        const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        if (!sfzReg.test(this.data.idCard)) {
            wx.showModal({
                showCancel:false,
                content: '请输入正确的身份证',
              })
            return
        }
        http.postHttp(api.addChild, {
                name: this.data.name,
                personal_id: this.data.idCard,
                user_id: this.data.user_id
            },
            (result) => {
                if (result.code == 0) {
                    wx.setStorageSync('child', result.data)
                    if (result.data.length > 0) {
                        wx.setStorageSync('childInfo', result.data[0])
                    } else {
                        wx.setStorageSync('childInfo', {})
                    }
                    this.init()
                } else {
                    wx.showModal({
                        showCancel:false,
                        content: "身份证不能重复",
                      })
                }

            },
            (err) => {
                console.log(err);
            },
        )
    },
    inputFocusFun() {
        this.setData({
            errorFlag: false,
            errorIdCardFlag: false
        })
    },
    init() {
        const child = wx.getStorageSync('child')
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        let i;
        child.forEach((ele, index) => {
            if (ele.id == childInfo.id) {
                ele.left = "137rpx"
                ele.opacity = "1"
                ele.big = "1"
                ele.animation = null
                i = index
            } else {
                if (i == 0) {
                    if (child[1]) {
                        child[1].left = "20rpx"
                        child[1].opacity = "0.8"
                        child[1].big = "0.7"
                        child[1].animation = null
                    }

                    if (child[2]) {
                        child[2].left = "255rpx"
                        child[2].opacity = "0.8"
                        child[2].big = "0.7"
                        child[2].animation = null
                    }

                } else if (i == 1) {
                    child[0].left = "20rpx"
                    child[0].opacity = "0.8"
                    child[0].big = "0.8"
                    child[0].animation = null
                    if (child[2]) {
                        child[2].left = "255rpx"
                        child[2].opacity = "0.8"
                        child[2].big = "0.8"
                        child[2].animation = null
                    }
                } else {
                    child[0].left = "20rpx"
                    child[0].opacity = "0.8"
                    child[0].big = "0.8"
                    child[0].animation = null
                    if (child[1]) {
                        child[1].left = "255rpx"
                        child[1].opacity = "0.8"
                        child[1].big = "0.8"
                        child[1].animation = null
                    }
                }
            }
        })
        this.setData({
            child: child || [],
            telephone: userInfo.telephone,
            user_id: userInfo.id,
            userInfo: userInfo,
            childInfo: childInfo
        })
        if (this.data.childInfo.id) {
            this.getChildQY()
        }
        wx.setStorageSync('child', child)
        this.__set__();
        this.move();
        if (this.data.idCard) {
            /** 添加孩子的时候检测有没有买产品，买了产品强制去激活权益 */
            http.postHttp(api.checkVIP, {
                    personal_id: this.data.idCard,
                    name: this.data.name
                },
                (result) => {
                    if (result.code == 0) {
                        if (result.data.if_vip) {
                            wx.navigateTo({
                                url: '/pages/activationInfo/index?childname=' + this.data.name +
                                    '&idcard=' + this.data.idCard + '&phone=' + this.data.telephone +
                                    '&from=my',
                            })
                        } else {
                            this.setData({
                                showPopup: false
                            })
                        }
                    }

                },
                (err) => {
                    console.log(err);
                },
            )
        }
    },
    /** 跳转档案页面 */
    toArchivesPage() {
        if (this.data.child.length == 0) {
            wx.showModal({
                showCancel:false,
                content: "请先添加孩子",
              })
            return
        }
        wx.navigateTo({
            url: '/pages/archivesPage/index',
        })
    },
    toAboutPage() {
        wx.navigateTo({
            url: '/pages/about/index',
        })
    },
    /** 跳转到激活权益页面 */
    jumpJihuo() {
        if (this.data.child.length == 0) {
            wx.showModal({
                showCancel:false,
                content: "请先添加孩子",
              })
            return
        }
        this.checkVIP()
    },
    /** 跳转到修改资料页面 */
    toUpdateZiliaoPage() {
        wx.navigateTo({
            url: '/packageA/pages/profileEdit/index',
        })
    },
    /** 跳转到我的预约页面 */
    toMyYY() {
        wx.navigateTo({
            url: '/packageA/pages/myYY/index',
        })
    },
    /** 跳转到测试页面 */
    toTestPage() {
        wx.navigateTo({
            url: '/packageA/pages/textPage/index',
        })
    },
    /** 跳转到我到权益 */
    toMyQuanyiPage() {
        wx.navigateTo({
            url: '/packageA/pages/myQuanyiPage/index',
        })
    },
    /** 查询个数 */
    getChildQY() {
        http.postHttp(api.getChildQY, {
                child_id: this.data.childInfo.id
            },
            (result) => {
                if (result.code == 0) {
                    this.setData({
                        total: result.data
                    })
                }

            },
            (err) => {
                console.log(err);
            },
        )
    },
    getUserInfo(openid) {
        http.getHttp(api.getUserInfo + openid, {},
            (result) => {
                if (result.code == 0) {
                    this.init()
                    wx.setStorageSync('child', result.data.child)
                    if (result.data.child.length > 0) {
                        const childInfo = wx.getStorageSync('childInfo')
                        if (childInfo.id == undefined) {
                            wx.setStorageSync('childInfo', result.data.child[0])
                        } else {
                            let arr = result.data.child
                            for (let i = 0; i < arr.length; i++) {
                                const ele = arr[i];
                                if (childInfo.if_vip != ele.if_vip && childInfo.id == ele.id) {
                                    console.log(
                                        111122
                                    );
                                    wx.setStorageSync('childInfo', ele)
                                    this.setData({
                                        childInfo: ele,
                                        child:result.data.child
                                    })
                                    this.init()
                                    break
                                }
                            }
                        }
                    } else {
                        wx.setStorageSync('childInfo', {})
                    }

                } else {
                    wx.clearStorage({
                        success: (res) => {
                            wx.redirectTo({
                                url: '/pages/loginHome/index',
                            })
                        },
                    })

                }
            },
            (err) => {
                console.log(err);
            }, {
                hideLoading: true
            }
        )

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {


    },
    move() {

        var datas = this.data.child;
        /*图片分布*/
        for (var i = 0; i < datas.length; i++) {
            var data = datas[i];
            var animation = wx.createAnimation({
                duration: 200
            });
            animation.translateX(data.left).scale(data.big).step();
            this.setData({
                ["child[" + i + "].animation"]: animation.export(),
                ["child[" + i + "].opacity"]: data.opacity,
                ["child[" + i + "].left"]: data.left,
                ["child[" + i + "].big"]: data.big,
            })
        }

    },
    __set__() {
        var that = this;
        var datas = that.data.child;
        for (var i = 0; i < datas.length; i++) {
            that.setData({
                ["order[" + i + "]"]: datas[i].idx
            })
        }
    },

    /**点击某项 */
    choose(e) {
        var idx = e.currentTarget.dataset.idx;
        var item = e.currentTarget.dataset.item;
        if (item.big == '1') {
            return
        }
        if (idx == 1) {
            this.data.child[0].left = "137rpx"
            this.data.child[0].opacity = "1"
            this.data.child[0].big = "1"
            this.data.child[0].animation = null
            if (this.data.child[1]) {
                this.data.child[1].left = "20rpx"
                this.data.child[1].opacity = "0.8"
                this.data.child[1].big = "0.8"
                this.data.child[1].animation = null
            }
            if (this.data.child[2]) {
                this.data.child[2].left = "255rpx"
                this.data.child[2].opacity = "0.8"
                this.data.child[2].big = "0.8"
                this.data.child[2].animation = null
            }
        }
        if (idx == 2) {
            if (this.data.child[1]) {
                this.data.child[1].left = "137rpx"
                this.data.child[1].opacity = "1"
                this.data.child[1].big = "1"
                this.data.child[1].animation = null
            }
            this.data.child[0].left = "20rpx"
            this.data.child[0].opacity = "0.8"
            this.data.child[0].big = "0.8"
            this.data.child[0].animation = null
            if (this.data.child[2]) {
                this.data.child[2].left = "255rpx"
                this.data.child[2].opacity = "0.8"
                this.data.child[2].big = "0.8"
                this.data.child[2].animation = null
            }
        }
        if (idx == 3) {
            if (this.data.child[2]) {
                this.data.child[2].left = "137rpx"
                this.data.child[2].opacity = "1"
                this.data.child[2].big = "1"
                this.data.child[2].animation = null
            }
            this.data.child[0].left = "20rpx"
            this.data.child[0].opacity = "0.8"
            this.data.child[0].big = "0.8"
            this.data.child[0].animation = null
            if (this.data.child[1]) {
                this.data.child[1].left = "255rpx"
                this.data.child[1].opacity = "0.8"
                this.data.child[1].big = "0.8"
                this.data.child[1].animation = null
            }
        }
        this.move();
        wx.setStorageSync('childInfo', item)
        this.setData({
            childInfo: item
        })
        this.getChildQY()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 4 // 选中的tabBar
            })
        }

        const openid = wx.getStorageSync('openid')
        this.getUserInfo(openid)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        this.setData({
            idCard: "",
            name: "",
            showPopup: false
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})